﻿namespace Arms.Presentation.Web.Common
{
    using Arms.Application.Modules.Security;
    using Newtonsoft.Json;
    using System;
    using System.Web.Mvc;
    using System.Web.Security;

    public abstract class ArmsWebViewPage<TModel> : WebViewPage<TModel>
    {
        public virtual new ArmsUser User
        {
            get { return base.User as ArmsUser; }
        }

        public virtual ArmsUserData UserData
        {
            get
            {
                var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authCookie == null) return null;

                // extract the forms authentication cookie
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                if (authTicket == null) return null;

                if (string.IsNullOrWhiteSpace(authTicket.UserData)) return null;

                // extract user data from ticket
                return JsonConvert.DeserializeObject<ArmsUserData>(authTicket.UserData);
            }
        }
    }
}
